package com.wms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.UserLoginDto;
import com.wms.mapper.UserMapper;
import com.wms.pojo.PageResult;
import com.wms.pojo.User;
import com.wms.service.UserService;
import com.wms.utils.JwtUtils;
import com.wms.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    //条件分页查询
    @Override
    public PageResult queryUserPage(UserVo vo) {
        PageHelper.startPage(vo.getPage(), vo.getPageSize());
        List<User> list = userMapper.queryUserPage(vo);
        Page<User> userPage = (Page<User>) list;
        return new PageResult(userPage.getTotal(), userPage.getResult());
    }

    //用户登录
    @Override
    public UserLoginDto queryUserLogin(User user) {
        User userInfo = getBaseMapper().selectOne(Wrappers.<User>lambdaQuery()
                .eq(User::getUsername, user.getUsername())
                .eq(User::getPassword, user.getPassword()));
        if (!ObjectUtils.isEmpty(userInfo)) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", userInfo.getId());
            map.put("name", userInfo.getName());
            map.put("username", userInfo.getUsername());
            String token = JwtUtils.getJwt(map);
            return new UserLoginDto(userInfo.getId(), userInfo.getName(), userInfo.getUsername(), token);
        }
        return null;
    }
}
